package com.whfc.hdc.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.util.PageUtil;
import com.whfc.hdc.dao.FshDeviceConfigMapper;
import com.whfc.hdc.dao.FshDeviceDataLogMapper;
import com.whfc.hdc.dao.FshDeviceDataMapper;
import com.whfc.hdc.dto.FshDeviceDataDTO;
import com.whfc.hdc.entity.FshDeviceData;
import com.whfc.hdc.manager.FshDeviceDataManager;
import com.whfc.hdc.service.FshDeviceDataService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-04-07 16:51
 */
@DubboService(interfaceClass = FshDeviceDataService.class, version = "1.0.0")
public class FshDeviceDataServiceImpl implements FshDeviceDataService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FshDeviceDataMapper fshDeviceDataMapper;

    @Autowired
    private FshDeviceDataLogMapper fshDeviceDataLogMapper;

    @Autowired
    private FshDeviceDataManager fshDeviceDataManager;

    @Autowired
    private FshDeviceConfigMapper fshDeviceConfigMapper;


    @Override
    public PageData<FshDeviceDataDTO> list(Integer pageNum, Integer pageSize, String keyword) throws BizException {
        logger.info("获取视频安全帽数据信息, pageNum:{}, pageSize:{}, keyword:{}", pageNum, pageSize, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<FshDeviceDataDTO> list = fshDeviceDataMapper.selectDeviceDataList(keyword);
        PageHelper.clearPage();
        for (FshDeviceDataDTO fshDeviceDataDTO : list) {
            String registrationId = fshDeviceConfigMapper.selectRegistrationId(fshDeviceDataDTO.getSn());
            int registerStatus = 0;
            if (StringUtils.isNotBlank(registrationId)) {
                registerStatus = 1;
            }
            fshDeviceDataDTO.setRegisterStatus(registerStatus);
            fshDeviceDataDTO.setLight(0);
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public PageData<FshDeviceDataDTO> getDataLog(String sn, Integer pageNum, Integer pageSize, Date startTime,
                                                 Date endTime) throws BizException {
        logger.info("获取视频安全帽历史数据, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}", sn, pageNum,
                pageSize, startTime, endTime);
        PageHelper.startPage(pageNum, pageSize);
        List<FshDeviceDataDTO> list = fshDeviceDataLogMapper.selectDataLogList(sn, startTime, endTime);
        PageHelper.clearPage();
        //处理报警记录
        for (FshDeviceDataDTO fshDeviceDataDTO : list) {
            String alarms = fshDeviceDataDTO.getAlarms();
            if (StringUtils.isNotBlank(alarms)) {
                List<Integer> alarmList = JSON.parseArray(alarms, Integer.class);
                fshDeviceDataDTO.setAlarmList(alarmList);
                fshDeviceDataDTO.setAlarms(null);
            }
        }
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNetState(Integer deviceId, String sn, Integer netState) throws BizException {
        logger.info("更新设备状态, deviceId:{}, sn:{}, netState:{}", deviceId, sn, netState);
        FshDeviceData fshDeviceData = fshDeviceDataMapper.selectBySn(sn);
        if (fshDeviceData == null) {
            //初始化设备配置
            fshDeviceDataManager.initDeviceConfig(sn);
            //创建设备数据
            fshDeviceData = new FshDeviceData();
            fshDeviceData.setDeviceId(deviceId);
            fshDeviceData.setSn(sn);
            fshDeviceData.setNetState(netState);
            fshDeviceDataMapper.insertSelective(fshDeviceData);
        } else {
            fshDeviceData.setNetState(netState);
            fshDeviceDataMapper.updateByPrimaryKeySelective(fshDeviceData);
        }
    }
}
